var map1 = null;

var scalebar1 = null;

var symbolUrl = "../img/alarm.gif";
var defaultEventIcon = "../img/defaultEventType.png";
var defaultTVIcon = "../img/defaultTV.png";
var defaultPartIcon = "../img/defaultPart.png";

var arcGisUrl = "http://39.107.125.44:6080";
var map;
var toolbar;
var partLayer;
var centerX = 106.638;// 120.206354450751;
var centerY = 26.684;// 30.0725834699399;
var currentGraphic;
var drawHandler;

var dynamicTowPointFiveD;
var extendScal = 10;
var graphic;
var registerPointType = "registerPointType";
var reportPointType = "reportPointType";
var tvPointType = "tvPointType";
var flickerPointType = "flickerPointType";
var partPointType = "partPointType";
var gridNetType = "gridNetType";

var newTempId = "registerTempId";
var flickerPointID = "flickerPointID";
var secondLayer;

var vec;
var veca;

require([ "esri/map", "esri/Color", "esri/graphic", "esri/InfoTemplate",
		"esri/SpatialReference", "esri/renderers/HeatmapRenderer",
		// 控件
		"esri/dijit/OverviewMap", "esri/dijit/Scalebar",
		"esri/dijit/Measurement", "esri/units",
		// 图层
		"tdlib/TDTLayer", "tdlib/TDTAnnoLayer", "tdlib/TDT_ImgLayer",
		"tdlib/TDT_ImgAnnoLayer", "esri/layers/GraphicsLayer",
		"esri/layers/ArcGISDynamicMapServiceLayer",
		"esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/FeatureLayer",
		// 图形要素
		"esri/geometry/Point", "esri/geometry/Polyline",
		"esri/geometry/Polygon", "esri/geometry/Circle",
		// 样式
		"esri/symbols/TextSymbol", "esri/symbols/SimpleMarkerSymbol",
		"esri/symbols/PictureMarkerSymbol", "esri/symbols/SimpleLineSymbol",
		"esri/symbols/SimpleFillSymbol", "esri/toolbars/draw" ], function(Map,
		Color, Graphic, InfoTemplate, SpatialReference, HeatmapRenderer,
		OverviewMap, Scalebar, Measurement, Units, TDTLayer, TDTAnnoLayer,
		TDT_ImgLayer, TDT_ImgAnnoLayer, GraphicsLayer,
		ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer, FeatureLayer,
		Point, Polyline, Polygon, Circle, TextSymbol, SimpleMarkerSymbol,
		PictureMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, Draw) {

	map1 = new Map("visitorReportHistory_allmap", {
		slider : false,
		logo : false,
		showLabels : true,
		center : [ centerX, centerY ],
		zoom : 17,
		minZoom : 8
	});

	scalebar1 = new Scalebar({
		map : map1,
		scalebarUnit : "dual",
		attachTo : 'top-center'
	});

	scalebar1.hide();
	drawHandler = Draw;
	vec = new TDTLayer();// 切片线画图
	veca = new TDTAnnoLayer();// 切片标注
	var img = new TDT_ImgLayer();
	var imga = new TDT_ImgAnnoLayer();
	map1.addLayer(vec);
	map1.addLayer(veca);

	clearInterval(flashTime);
	// 单位数据刷新
	var flashTime = setInterval(function() {
		refreashFlickerPoint();
	}, 75);

});

function refreashFlickerPoint() {
	if (flickerPoint) {
		var symbol = flickerPoint.symbol;

		symbol.color.a -= 0.05;
		symbol.setSize(symbol.size + 8);

		if (symbol.color.a <= 0) {
			symbol.color.a = 1;
			symbol.setSize(1);
		}
		flickerPoint.setSymbol(symbol);
	}
}
var flickerPoint;



function createFlickerPoint(data, str) {

	var symbol = new esri.symbol.SimpleMarkerSymbol(
			esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, // 样式,STYLE_CIRCLE(●),STYLE_CROSS(+),STYLE_DIAMOND(◆),STYLE_SQUARE(■),STYLE_X(X)
			1, // 像素
			new esri.symbol.SimpleLineSymbol(
					esri.symbol.SimpleLineSymbol.STYLE_SOLID, // 样式,STYLE_DASH(破折号),STYLE_DASHDOT(点划线),STYLE_DASHDOTDOT,STYLE_DOT(点),STYLE_NULL,STYLE_SOLID(实线)
					new dojo.Color([ 255, 0, 0 ]), 0.1), // 颜色 1 // 像素 ),
			new dojo.Color([ 255, 0, 0, 1 ]) // 颜色和透明度
	);
	var myPoint = {
		"geometry" : {
			"type" : "point",
			"x" : data.longitude,
			"y" : data.latitude,
			"spatialReference" : {
				"wkid" : 4326
			}
		},
		"attributes" : data,
		"symbol" : symbol
	};
	var graphic = getcurrentGraphic(data.id, str);

	if (graphic == null) {
		graphic = new esri.Graphic(myPoint);
		graphic.symbol = symbol;
		graphic.attributes = data;

	} else {
		var geometry = graphic.geometry;
		geometry.x = data.longitude;
		geometry.y = data.latitude;

		graphic.attributes = data;
		graphic.setGeometry(geometry);
		graphic.setSymbol(symbol);
	}

	if (str == "#visitorReportHistory") {
		map1.graphics.add(graphic);
	}
	
	flickerPoint = graphic;

	return graphic;
};

function setCenter(str) {
	if (str == "#visitorReportHistory") {
		map1.centerAndZoom(flickerPoint.geometry, map1.getLevel());
	}
	
}

function getcurrentGraphic(data, str) {

	var graphics;

	if (str == "#visitorReportHistory") {
		graphics = map1.graphics.graphics;
	}
	

	for (var i = 0; i < graphics.length; i++) {
		if (graphics[i]) {
			if (graphics[i].attributes) {
				if (graphics[i].attributes) {
					return graphics[i];
				}
			}
		}
	}
	return null;
};
$(function() {
	// 上报历史
	initHistory();
	// 已受理
	initHasDid();
	//已完结
	initOvers();
	//待受理
	initInit();
});
//=================================初始化===============================
function initInit(){
	$.ajax({
		url : rootPath + "/ui/visitorTask/initInit?device=mobile&identify=",
		dataType : 'json',
		type : 'get',
		success : function(result) {
			if (result.code == 1) {
				var info = result.data.inits;
				buildPage("#visitorInit", info);
			}
		}
	});
}
//===============================完结和归档==============================
function initOvers(){
	$.ajax({
		url : rootPath + "/ui/visitorTask/initOvers?device=mobile&identify=",
		dataType : 'json',
		type : 'get',
		success : function(result) {
			if (result.code == 1) {
				var info = result.data.overs;
				buildPage("#visitorOvers", info);
			}
		}
	});
}
//===============================已受理==============================
function initHasDid(){
	$.ajax({
		url : rootPath + "/ui/visitorTask/initHasDid?device=mobile&identify=",
		dataType : 'json',
		type : 'get',
		success : function(result) {
			if (result.code == 1) {
				var info = result.data.hasDid;
				buildPage("#visitorHasDid", info);
			}
		}
	});
}
// ===============================上报历史===============================
function initHistory() {

	$.ajax({
		url : rootPath + "/ui/visitorTask/initHistory?device=mobile&identify=",
		dataType : 'json',
		type : 'get',
		success : function(result) {
			if (result.code == 1) {
				var info = result.data.histories;
				buildPage("#visitorReportHistory", info);
			}
		}
	});
};
//================================上报数据======================================
function buildPage(str,data){
	$(str).empty();
	if (data.length == 0) {
		var contentval = '<li><h2 style="color:blue">暂无事件信息</h2></li>';
		$(str).append(contentval);
		$(str).listview('refresh');
	}
	for (var i = 0; i < data.length; i++) {
		
		var purl = data[i].reportImgAdress;

		if (purl == undefined || purl == "") {
			purl = "img/nopicture.png";
		}
		purl = window.rootPath + "/" + purl;
		var eventTaskMemo=data[i].reportMemo;
		if(eventTaskMemo==''||undefined==eventTaskMemo){
			eventTaskMemo="该事件无备注";
		}
		var s='<img width="4%" src="/wcsi/img/yourlocation.png"/>'+data[i].reportAdress;
		var contenthtml='<li><a style="background-color:#FFFFFF;" href="#" data-transition="slide" defindStr="'+str+'" class="hrefClickR" defineId="'+data[i].id+'" id="'+data[i].id+'">'
		+ '<div class="listViewImg">'
		+ '<img src="'+purl+'"/>'
		+ '</div>'
		+ '<div class="listViewContent">'
		+ '<p class="listViewTitle margin-Title"><font color="red">'+moment(data[i].reportAccTime).format('YYYY-MM-DD HH:mm')+'</font></p>'
		+ '<p class="listViewMemo margin-Title">&nbsp;'+data[i].reportEventType.eventName
		+ '<br/>&nbsp;'+eventTaskMemo+'<br/>'+s+'</p>'
		+ '</div>';
		contenthtml+='</a></li>';
		$(str).append(contenthtml);
	}
	$(str).listview('refresh');
//	for(var i=0;i<data.length;i++){
//		$("#"+data[i].id).removeClass("ui-btn-icon-right");
//	}
	$('.ui-btn-icon-right').removeClass('ui-btn-icon-right');
};
//===========================事件源详情===================================
$(document).on("click",".hrefClickR",function() {
	var str=$(this).attr("defindStr");
	str="#visitorReportHistory";
	var id = $(this).attr("defineId");
	//alert(str);
	$.ajax({
		url : rootPath+ "/ui/visitorTask/findReportByReportId/"+ id,
		dataType : 'json',
		type : 'get',
		success:function(result){
			if(result.code==1){
				var data=result.data.report;
				var content=data.reportContent;
				if(content==''||undefined==content||content==null){
					content='未上报内容';
				}
				$(str + "_registerEventContent").html(content);
				var reportMemo=data.reportMemo;
				if(reportMemo==''||undefined==reportMemo||reportMemo==null){
					reportMemo='未上报备注';
				}
				$(str + "_registerEventMemo").html(reportMemo);
				
				$(str + "_registerEventType").html(data.reportEventType.eventName);
				
				$(str + "_registerEventAccTime").html(moment(data.reportAccTime).format("YYYY-MM-DD HH:mm"));
				//$(str + "_registerEventCreateUser").html(data.reportUser.name);
				var where=data.reportAdress;
				if(where==null||''==where||undefined==where){
					where="上报时未获取到上报地点";
				}
			
				$(str + "_where").html(where);
				createFlickerPoint(data, str);
				setTimeout(function() {
					setCenter(str);
				}, 500);

			}
		}
	});
	$(this).prop("href",   str+"_mb");
});
